package com.example.teesystem.controller;

import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.wrongSet.WrongSetSelectVo;
import com.example.teesystem.entity.vo.wrongSet.WrongSetUpdateVo;
import com.example.teesystem.service.WrongSetService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/4/7 15:46
 */
@RestController
@RequestMapping(value = "/tee/wrongSet" , produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor
public class WrongSetController {
    private final WrongSetService wrongSetService;

    /**
     * 添加题目到错题集
     * @param map
     * @return
     */
    @PostMapping(value = "/insertWrongSet")
    public Response insertWrongSet(@Valid @RequestBody Map<String, String> map){
        int issueId;
        try {
            issueId = Integer.parseInt(map.get("issueId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return wrongSetService.insertToWrongSet(issueId);
    }

    /**
     * 分页查询错题
     * @param wrongSetSelectVo
     * @return
     */
    @PostMapping(value = "/getWrongSet")
    public Response getIssueByCourseId(@Valid @RequestBody WrongSetSelectVo wrongSetSelectVo){
        try{
            return Response.ok(wrongSetService.getAllWrongSet(wrongSetSelectVo));
        }catch (Exception e){
            return Response.error(ResultCode.GET_ERROR.getMsg(), ResultCode.GET_ERROR.getCode());
        }
    }

    /**
     * 回答错题
     * @param wrongSetUpdateVo
     * @return
     */
    @PostMapping(value = "/updateWrongSet")
    public Response updateWrongSet(@Valid @RequestBody WrongSetUpdateVo wrongSetUpdateVo){
        return wrongSetService.updateWrongSet(wrongSetUpdateVo);
    }

    /**
     * 删除错题集中的题目
     * @param map
     * @return
     */
    @PostMapping(value = "/deleteWrongSet")
    public Response deleteWrongSet(@RequestBody Map<String, String> map){
        int id;
        try{
            id = Integer.parseInt(map.get("id"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        return wrongSetService.deleteWrongSet(id);
    }

    @PostMapping(value = "/wrongSetTraining")
    private Response wrongSetTraining(@RequestBody Map<String,String> map){
        Integer courseId;
        try{
            courseId = Integer.parseInt(map.get("courseId"));
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return wrongSetService.getWrongSetByOtherMessage(courseId);
    }
}
